﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class ZhiChengServices : IZhiChengServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public ZhiChengServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> AddZhiCheng(ZhiCheng zhiCheng)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"insert into config_public_char(attribute_kind,attribute_name) values('{zhiCheng.Attribute_kind}','{zhiCheng.Attribute_name}')");
                }
            });
        }

        public Task<int> DelZhiCheng(int pbc_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"delete from config_public_char where pbc_id='{pbc_id}'");
                }
            });
        }

        public Task<List<ZhiCheng>> FindZhiCheng()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<ZhiCheng>($"select * from config_public_char where attribute_kind='职称'").ToList();
                }
            });           
        }

        public Task<int> UpdateZhiCheng(ZhiCheng zhiCheng)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update config_public_char set attribute_kind='{zhiCheng.Attribute_kind}',attribute_name='{zhiCheng.Attribute_name}' where pbc_id='{zhiCheng.Pbc_id}'");
                }
            });
        }
    }
}
